<template>
	<view>
		<view class="user-info-container">
			<view class="user-info-item">
				<text>昵称：</text>
				<input type="text" class="update-input" v-model="userInfo.nickname" />
			</view>
			<view class="user-info-item">
				<text>性别：</text>
				<input type="text" class="update-input"  v-model="userInfo.sex" />
			</view>
			<view class="user-info-item">
				<text>邮箱：</text>
				<input type="text" class="update-input"  v-model="userInfo.userEmail" />
			</view>
			<view class="user-info-item">
				<text>真名：</text>
				<input type="text" class="update-input"  v-model="userInfo.name" />
			</view>
			<button type="primary" class="update-info" @click="updateInfo">修改信息</button>
		</view>
	</view>
</template>

<script>
	import userApi from '@/api/myUser.js'
	export default {
		data() {
			return {
				user: uni.getStorageSync('user'),
				userInfo: {
					username: '',
					nickname: '',
					sex: '',
					userEmail: '',
					name: ''
				}
			}
		},
		onShow() {
			this.userInfo.userId = this.user.userId
			this.getById()
		},
		watch: {
			
		},
		methods: {
			// 获取用户信息
			getById() {
				userApi.get(this.user.userId).then(res => {
					this.userInfo.userId = res.data.userId;
					this.userInfo.username = res.data.username;
					this.userInfo.nickname = res.data.nickname;
					this.userInfo.sex = res.data.sex;
					this.userInfo.userEmail = res.data.userEmail;
					this.userInfo.name = res.data.name;
					if(this.userInfo.sex === '0') {
						this.userInfo.sex = "男"
						console.log(this.userInfo.sex)
					}else {
						this.userInfo.sex = "男"
						console.log(this.userInfo.sex)
					}
				})
			},
			updateInfo() {
				userApi.updateInfo(this.userInfo).then(res => {
					console.log(res)
					uni.showToast({
						title: '更新成功',
						icon: 'none'
					})
				})
				this.getById()
				uni.navigateTo({
					url: '/pages/update-info/update-info'
				})
			}
		}
	}
</script>

<style>
@import url('./index.css')
</style>
